package com.nbk.composition

import com.nbk.composition.bean.Response
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.HttpRequestMethodNotSupportedException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Author:  LuoHaoLun
 * Email :  506503279@qq.com
 * Date  :  2020/1/1
 */
@ControllerAdvice
class GlobalException {

    @ExceptionHandler
    @ResponseBody
    fun errorHandler(e: Throwable) = Response(false).apply {
        e.printStackTrace()
        msg = when (e) {
            is HttpMessageNotReadableException -> "参数错误"
            is HttpRequestMethodNotSupportedException -> "请求方法错误"
            is HandleError -> e.message ?: "未知错误：$e"
            else -> {
//                logRepository.save(SystemLog(request.getAttribute("userId") as String? ?: "", LOG_TYPE_SYSTEM_ERROR, e.toString(), getCurrentTime()))
                "其它错误：${e.message}"
            }
        }
    }

}